/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.entity.*;
import com.jeesite.modules.st.entity.formula.StFormulaNameChild;
import com.jeesite.modules.st.pojo.AjaxJson;
import com.jeesite.modules.st.service.StCustomRecordService;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StProductRecordService;
import com.jeesite.modules.st.service.formula.StFormulaNameService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.service.StSaleBillService;

import java.util.List;

/**
 * 销售单Controller
 *
 * @author HOHD
 * @version 2020-12-18
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stSaleBill")
public class StSaleBillController extends BaseController {

    @Autowired
    private StSaleBillService stSaleBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StSaleBill get(String id, boolean isNewRecord) {
        return stSaleBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StSaleBill stSaleBill, Model model) {
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillList";
    }

    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = {"forChoose"})
    public String forChoose(StSaleBill stSaleBill, Model model) {
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillList-4-choose";
    }

    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = {"detail"})
    public String detail(StSaleBill stSaleBill, Model model) {
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillList-detail";
    }
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = {"listSaleDetail"})
    public String listSaleDetail(StSaleBill stSaleBill, Model model) {
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillList-list-sale-detail";
    }
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = {"saleStatistics"})
    public String saleStatistics(StSaleBill stSaleBill, Model model) {
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillList-sale-statistics";
    }


    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StSaleBill> listData(StSaleBill stSaleBill, HttpServletRequest request, HttpServletResponse response) {
        stSaleBill.setPage(new Page<>(request, response));
        Page<StSaleBill> page = stSaleBillService.findPage(stSaleBill);
        return page;
    }

    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "listDetail")
    @ResponseBody
    public List<StSaleBillDetail> listDetail(StSaleBill stSaleBill) {
        return stSaleBillService.findDetail(stSaleBill);
    }

    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "showDetail")
    @ResponseBody
    public Page<StSaleBillDetail> showDetail(StSaleBillDetail stSaleBillDetail, HttpServletRequest request, HttpServletResponse response) {
        stSaleBillDetail.setPage(new Page<>(request, response));
        return stSaleBillService.showDetail(stSaleBillDetail);
    }
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "listSaleDetailData")
    @ResponseBody
    public Page<StSaleBillDetail> listSaleDetailData(StSaleBillDetail stSaleBillDetail, HttpServletRequest request, HttpServletResponse response) {
        stSaleBillDetail.setPage(new Page<>(request, response));
        return stSaleBillService.listSaleDetail(stSaleBillDetail);
    }
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "saleStatisticsData")
    @ResponseBody
    public Page<StSaleBillDetail> saleStatisticsData(StSaleBillDetail stSaleBillDetail, HttpServletRequest request, HttpServletResponse response) {
        stSaleBillDetail.setPage(new Page<>(request, response));
        return stSaleBillService.saleStatistics(stSaleBillDetail);
    }

    @Autowired
    StCustomRecordService stCustomRecordService;

    @Autowired
    StProductRecordService stProductRecordService;
    @Autowired
    StFormulaNameService stFormulaNameService;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stSaleBill:view")
    @RequestMapping(value = "form")
    public String form(StSaleBill stSaleBill, Model model) throws JsonProcessingException {
        StFormulaNameChild entity = new StFormulaNameChild();
        List<StFormulaNameChild> formulaList = stFormulaNameService.findList(entity);

        model.addAttribute("formulaList", objectMapper.writeValueAsString(formulaList));
        List<StCustomRecord> customList = stCustomRecordService.findList(new StCustomRecord());
        model.addAttribute("customList", customList);

        List<StProductRecord> productList = stProductRecordService.findList(new StProductRecord());
        model.addAttribute("productList", objectMapper.writeValueAsString(productList));
        model.addAttribute("stSaleBill", stSaleBill);
        return "modules/st/stSaleBillForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stSaleBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StSaleBill stSaleBill) {
        stSaleBillService.save(stSaleBill);
        return renderResult(Global.TRUE, text("保存销售单成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stSaleBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StSaleBill stSaleBill) {
        stSaleBillService.delete(stSaleBill);
        return renderResult(Global.TRUE, text("删除销售单成功！"));
    }

}